/*
*
* PollMaker
*
* This class takes the data extracted from KMLReader and creates a
* Poll object for each row
*
*/
class PollMaker {
  
  // attributes
  String[] nameArray, pollidArray, shortNameArray, psbWardArray, csbWardArray, coordStringArray;
  String[][] electionDataArray;
  
  // ----------------------------------------
  // constructor method
  // ----------------------------------------
  PollMaker(String[][] fromKML, String[][] fromElection) {
    
   nameArray = fromKML[0];
   pollidArray = fromKML[1];
   shortNameArray = fromKML[2];
   psbWardArray = fromKML[3];
   csbWardArray = fromKML[4];
   coordStringArray = fromKML[5];
   electionDataArray = fromElection;   
    
  } // constructor method
  
  // -----------------------------------------
  // makePolls
  // this method creates an array of Poll objects for output
  // -----------------------------------------
  Poll[] makePolls() {
  
    // initiate output array
    Poll[] out = new Poll[(nameArray.length)];
    
    // create array of poll shortnames from electionDataArray
    // note: first column is ward, second column is poll number
    int[][] csvShortNames = new int[electionDataArray.length][2];
    String[] csvLongName;
    for(int i = 1; i < electionDataArray.length; i++) {
      csvLongName = split(electionDataArray[i][6], ' ');
      csvShortNames[i][0] = int(csvLongName[1]);
      csvShortNames[i][1] = int(split(csvLongName[4], "\"")[0]);      
    } // for
    
    // iterate through rows of data and create NHood object for each
    for(int i = 0; i < nameArray.length; i++) {
       
      // split entry from coordinate strings array on colon
      // this will only result in more than item if the neighbour hood
      // in question is composed of more than one non-contiguous polygon
      String[] coordStrings = split(coordStringArray[i], ":");
      ArrayList coordOut = new ArrayList();
      // iterate through the list of polygons
      for(int j = 0; j < coordStrings.length; j++) {
        
        // split the string of coordinates for each polygon into
        // individual 3-element coordinate lines
        String[] coordStringsArray = split(coordStrings[j], ' ');
        
        PVector[] coords = new PVector[coordStringsArray.length - 1];
        PVector ll = new PVector();
        
        // iterate through each line
        for (int k = 0; k < (coordStringsArray.length - 1); k++) { // note: use coordStringsArray.length - 1 b/c last coord has trailing space
          // split each line into individual elements
          String[] h = split(coordStringsArray[k], ',');
          
          // convert each element to UTM coordinates and store
          ll.set( float(h[0]), float(h[1]), 0);
          coords[k] = utmConverter.latLongToUTM(ll);
        } // for loop
        
        // add coordinate matrix to output ArrayList
        coordOut.add(coords);
      } // for loop
      
      // create Poll object
      out[i] = new Poll(trim(nameArray[i]).toUpperCase(), pollidArray[i], shortNameArray[i], psbWardArray[i], csbWardArray[i], coordOut);
      
      // declare variables for below
      String[] kmlShortName;
      kmlShortName = split(shortNameArray[i], '-');
      float wardKML, wardCSV, pollKML, pollCSV;
      wardKML = int(kmlShortName[0]);
      pollKML = int(kmlShortName[1]);
      
      // search ElectionDataArray for poll's election data and import to Poll object
      for(int m = 1; m < electionDataArray.length; m++) {
        
        // compare KML and CSV poll names; if match, import data to Poll object
        if((wardKML == csvShortNames[m][0]) && (pollKML == csvShortNames[m][1])) {
          out[i].importElectionData(electionDataArray[m]);
        } // if
        
      } // for
      
      // generate each poll's calculated metrics
      out[i].genCalculatedMetrics();
      
    } // for 
    
  return out;
  
  } // makePolls method
  
} // class
